package no.uib.hplims.views.experiments;

import java.util.ArrayList;
import java.util.Collection;

import no.uib.hplims.Controller;
import no.uib.hplims.MyVaadinApplication;
import no.uib.hplims.models.Experiment;
import no.uib.hplims.models.Peptide;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class AddToExperimentWindow extends Window {
	private static final long serialVersionUID = -1671005583212355528L;
	
	private Button okButton = null;
	private Button newExperimentButton = null;
	private ComboBox experimentSelect = null;
	
	private Controller controller = null;
	
	private Collection<Peptide> peptides = null;

	public AddToExperimentWindow(Collection<Peptide> peptides) {
		super("Add to experiment");
		center();
		setModal(true);
		setWidth("400px");
		
		this.peptides = peptides;
		
		addComponent(new Label("You have selected " + peptides.size() + " peptides."));
		
		controller = MyVaadinApplication.getInstance().getController();
		
		ArrayList<Experiment> allExps = controller.getAllExperiments();
		Experiment current = controller.getCurrentExperiment();
		
		experimentSelect = new ComboBox("Please select an experiment", allExps);
		experimentSelect.setImmediate(true);
		
		// Set a reasonable width
		experimentSelect.setWidth(350, UNITS_PIXELS);
		// Set the appropriate filtering mode for this example
        experimentSelect.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
        experimentSelect.setNullSelectionAllowed(false);
		experimentSelect.addListener(valueChangeListener);
		experimentSelect.select(current);
		
		addComponent(experimentSelect);
		
		newExperimentButton = new Button("Create new experiment");
		newExperimentButton.addListener(clickListener);
		
		addComponent(newExperimentButton);
		
		okButton = new Button("OK");
		okButton.addListener(clickListener);
		
		addComponent(okButton);
	}
	
	private Button.ClickListener clickListener = new Button.ClickListener() {
		private static final long serialVersionUID = 5562079324940087934L;

		public void buttonClick(ClickEvent event) {
			if (event.getButton() == okButton) {
				
				okButtonClick();
				
			} else if (event.getButton() == newExperimentButton) {
				
				newExperimentButtonClick();
				
			}
		}
	};

	private Property.ValueChangeListener valueChangeListener = new Property.ValueChangeListener() {
		private static final long serialVersionUID = -2487243421799451196L;

		public void valueChange(ValueChangeEvent event) {
			controller.setCurrentExperiment((Experiment) event.getProperty().getValue());
		}
	};

	private void okButtonClick() {
		Experiment e = controller.getCurrentExperiment();
		if (e != null) {
			for (Peptide p : peptides) {
				e.addPeptide(p.getId());
			}
			MyVaadinApplication.getInstance().getController().updateExperiment(e);
			close();
		}
	}
	
	private void newExperimentButtonClick() {
		Experiment e = controller.createNewExperiment();
		experimentSelect.addItem(e);
		experimentSelect.select(e);
	}

}
